All files / src/app/api/admin/testimonials/[id] route.ts

100% Statements 180/180
100% Branches 25/25
100% Functions 4/4
100% Lines 180/180

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 1811x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x 3x 3x 3x 2x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 1x 1x 5x 5x 5x 5x 5x 6x 1x 1x 4x 4x 4x 4x 4x 4x 4x 6x 1x 1x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x 4x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 1x 1x 4x 4x 4x 4x 4x 5x 1x 1x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x  
export const dynamic = "force-dynamic";
 
import { NextRequest, NextResponse } from 'next/server';
import { } from "next-auth";
import { prisma } from "@/lib/prisma";
import { logger } from "@/lib/logging";
import { z } from "zod";
import {
  withAdmin,
  withErrorHandling,
  successResponse,
  ApiError,
  ApiSuccessResponse,
  ApiErrorResponse } from "@/lib/api";
import { RouteContext } from "@/lib/api/middleware";
 
const LOG_CATEGORY = "ADMIN_TESTIMONIALS_API";
 
// Validation schema for updating testimonials
const updateTestimonialSchema = z.object({
  review: z.string().min(10, "Review must be at least 10 characters").optional(),
  authorName: z.string().min(2, "Author name must be at least 2 characters").optional(),
  authorRole: z.string().min(2, "Author role must be at least 2 characters").optional(),
  authorImage: z
    .string()
    .url("Author image must be a valid URL")
    .or(z.string().startsWith("/"))
    .optional(),
  isActive: z.boolean().optional() });
 
interface RouteParams {
  params: Promise<{ id: string }>;
}
 
/**
 * GET /api/admin/testimonials/[id]
 * Get a single testimonial
 */
async function handleGet(_request: NextRequest,
  context: RouteContext | undefined): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const { id } = await (context as RouteParams).params;
  const testimonialId = parseInt(id);
 
  if (isNaN(testimonialId)) {
    throw ApiError.badRequest("Invalid testimonial ID");
  }
 
  const testimonial = await prisma.testimonial.findUnique({
    where: { id: testimonialId } });
 
  if (!testimonial) {
    throw ApiError.notFound("Testimonial");
  }
 
  return successResponse({
    id: testimonial.id,
    review: testimonial.review,
    authorName: testimonial.authorName,
    authorRole: testimonial.authorRole,
    authorImage: testimonial.authorImage,
    isActive: testimonial.isActive,
    createdAt: testimonial.createdAt });
}
 
/**
 * PUT /api/admin/testimonials/[id]
 * Update a testimonial
 */
async function handlePut(request: NextRequest,
  context: RouteContext | undefined): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const { id } = await (context as RouteParams).params;
  const testimonialId = parseInt(id);
 
  if (isNaN(testimonialId)) {
    throw ApiError.badRequest("Invalid testimonial ID");
  }
 
  const body = await request.json();
 
  // Validate input
  const validationResult = updateTestimonialSchema.safeParse(body);
  if (!validationResult.success) {
    throw ApiError.validation("Validation failed", validationResult.error.issues);
  }
 
  const validatedData = validationResult.data;
 
  // Check if testimonial exists
  const existing = await prisma.testimonial.findUnique({
    where: { id: testimonialId } });
 
  if (!existing) {
    throw ApiError.notFound("Testimonial");
  }
 
  const testimonial = await prisma.testimonial.update({
    where: { id: testimonialId },
    data: validatedData });
 
  logger.info(`Updated testimonial ${testimonialId}`, { category: LOG_CATEGORY });
 
  return successResponse({
    id: testimonial.id,
    review: testimonial.review,
    authorName: testimonial.authorName,
    authorRole: testimonial.authorRole,
    authorImage: testimonial.authorImage,
    isActive: testimonial.isActive,
    createdAt: testimonial.createdAt });
}
 
/**
 * DELETE /api/admin/testimonials/[id]
 * Delete a testimonial
 */
async function handleDelete(_request: NextRequest,
  context: RouteContext | undefined): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const { id } = await (context as RouteParams).params;
  const testimonialId = parseInt(id);
 
  if (isNaN(testimonialId)) {
    throw ApiError.badRequest("Invalid testimonial ID");
  }
 
  // Check if testimonial exists
  const existing = await prisma.testimonial.findUnique({
    where: { id: testimonialId } });
 
  if (!existing) {
    throw ApiError.notFound("Testimonial");
  }
 
  await prisma.testimonial.delete({
    where: { id: testimonialId } });
 
  logger.info(`Deleted testimonial ${testimonialId}`, { category: LOG_CATEGORY });
 
  return successResponse({ message: "Testimonial deleted successfully" });
}
 
/**
 * PATCH /api/admin/testimonials/[id]
 * Toggle testimonial active status
 */
async function handlePatch(_request: NextRequest,
  context: RouteContext | undefined): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const { id } = await (context as RouteParams).params;
  const testimonialId = parseInt(id);
 
  if (isNaN(testimonialId)) {
    throw ApiError.badRequest("Invalid testimonial ID");
  }
 
  // Get current testimonial
  const existing = await prisma.testimonial.findUnique({
    where: { id: testimonialId } });
 
  if (!existing) {
    throw ApiError.notFound("Testimonial");
  }
 
  // Toggle active status
  const testimonial = await prisma.testimonial.update({
    where: { id: testimonialId },
    data: { isActive: !existing.isActive } });
 
  logger.info(
    `Toggled testimonial ${testimonialId} active status to ${testimonial.isActive}`,
    { category: LOG_CATEGORY }
  );
 
  return successResponse({
    id: testimonial.id,
    isActive: testimonial.isActive });
}
 
export const GET = withErrorHandling(withAdmin(handleGet));
export const PUT = withErrorHandling(withAdmin(handlePut));
export const DELETE = withErrorHandling(withAdmin(handleDelete));
export const PATCH = withErrorHandling(withAdmin(handlePatch));